// article JavaScript Document

$(function() {
	var isUnderIe6 = uaInfo.isIe && uaInfo.ieVr <= 6,
		isUnderIe7 = uaInfo.isIe && uaInfo.ieVr <= 7,
		isUnderIe8 = uaInfo.isIe && uaInfo.ieVr <= 8;

	/*******************************************************
	 *　モーダルマップ
	 *******************************************************/
	var modalMap = new APMN.Modal({
		target: $('#modal_map'),
		close: $('#modal_map .modal_close'),
		ieVr: !(isUnderIe6)
		});

	/*modalMap実行*/
	$('.open_modal_map a').on('click' , function(e){
		e.preventDefault();
		modalMap.show();
	});
	
	/*******************************************************
	 *　tooltip
	 *******************************************************/
	$('.tooltip').powerTip({
		placement: isUnderIe6 ? 'e' : 'n'
	});
	$('.tooltip.zoom').each(function(i){
		$(this).data('powertiptarget', $(this).next().attr('id'));
	});


	/*******************************************************
	 *　まとめてお問い合わせ
	 *******************************************************/
	var $aBtn = $('.mod_box_inquiry_all .button a'),
		SelectMax = 20;

	function changeInquiryBtn(b) {
		if (b) {
			$aBtn.removeClass('mod_btn_disabled');
		} else {
			$aBtn.addClass('mod_btn_disabled');
		};
	};

	$aBtn.on('click', function(e){
		if (isSelected) {
			var _num = 0;
			$('.type_list_wrap .mod_check').each(function(i){
				if ($(this).find('input').prop('checked')) {
					_num ++;
				};
			});
			if (SelectMax < _num) {
				e.preventDefault();
				modalOverSelect.show();
			};
		} else {
			e.preventDefault();
		};
	});

	var modalOverSelect = new APMN.Modal({
		target: $('#modal_gomen'),
		close: $('#modal_gomen .modal_close'),
		ieVr: !(isUnderIe6)
	});


	/*******************************************************
	 *　Inputボタン
	 *******************************************************/
	var isSelected = null,
		selectDispath = false,
		$selectAll = $('.mod_box_inquiry_all'),
		$selectOne = $('.type_list_wrap');

	if (!isUnderIe8) {
		$selectAll.find('input')
			.on('ifChecked', function(e){
				if (selectDispath) return;
				selectDispath = true;
				$selectOne.find('input').iCheck('check');
				changeCheckBox();
				selectDispath = false;
			})
			.on('ifUnchecked', function(e){
				if (selectDispath) return;
				selectDispath = true;
				$selectOne.find('input').iCheck('uncheck');
				changeCheckBox();
				selectDispath = false;
			});
		$selectOne.find('input')
			.on('ifChecked', function(e){
				if (selectDispath) return;
				selectDispath = true;
				changeCheckBox();
				selectDispath = false;
			})
			.on('ifUnchecked', function(e){
				if (selectDispath) return;
				selectDispath = true;
				if ($selectAll.find('.mod_check').hasClass('mod_checked')) {
					$selectAll.find('input').iCheck('uncheck');
				};
				changeCheckBox();
				selectDispath = false;
			});
	} else {
		$selectAll.find('input')
			.on('click', function(e){
				var b = $(this).prop('checked');
				$selectOne.find('input').prop('checked', b);
				changeCheckBox();
			});
		$selectOne.find('li input')
			.on('click', function(e){
				var b = $(this).prop('checked');
				if (!b) {
					$selectAll.find('input').prop('checked', false);
				}
				changeCheckBox();
			});
	}

	function changeCheckBox(){
		var _isSlt = false;
		$('.mod_check').each(function(e){
			if ($(this).find('input').prop('checked')) {
				_isSlt = true;
				return false;
			};
		});
		if (isSelected != _isSlt) {
			isSelected = _isSlt;
			if (isSelected) {
				changeInquiryBtn(true);
			} else {
				changeInquiryBtn(false);
			}
		};
	};
	changeCheckBox();


	/*******************************************************
	 * 検証リストに追加
	 *******************************************************/
	
	$('.type_list_wrap .mod_table').find('a.mod_icn_star').on('click', function(e){
		e.preventDefault();
		if (!$(this).hasClass('mod_icn_star_active')) {
			$(this).addClass('mod_icn_star_active');
			addStar.show();
			
			//検討リスク
			KENTO.addKentoList($(this).prev().val());
		};
	});
	
	$('.btn_add a.mod_btn_s').on('click', function(e){
		e.preventDefault();
		if (!$(this).hasClass('mod_btn_active')) {
			$('.btn_add a.mod_btn_s').addClass('mod_btn_active');
			addStar.show();
			
			//検討リスク
			KENTO.addKentoList($(this).prev().val());
		};
	});

	var addStar = new APMN.ModalStar({
		path: '/static/shared/images/img_add_star.png',
		ieVr: !(isUnderIe6)
	});

	/*******************************************************
	 * map
	 *******************************************************/	
	var $mapOpnBtn = $('.around_map .open_menu'),
	$mapOpnTrg = $('.around_map .map');
	var mapOpener = new APMN.ToggleOpener({
		btn: $mapOpnBtn,
		target: $mapOpnTrg,
		isOpen: $('.around_map .open_menu').hasClass('active')
	});
	mapOpener.toggle = function(b) {
		if (b) $mapOpnBtn.addClass('active');
		else $mapOpnBtn.removeClass('active');
	};
	
	function makeMap() {
		var x = parseFloat($('.map').attr('lat'))*3600;
		var y = parseFloat($('.map').attr('lon'))*3600;
		var scl = 400;
	
		var map = new Mapple.SingleMap("map", 880, 400, x, y, scl, {
			k1: "apanew",
			k2: "1205jdwa",
			scaleOperateVisible: false ,scaleOperate: false,rotateOperateVisible: false 
		});
	
		if ($('#displayIcon').val() === '1') {
			var iconLayer = map.createOverlayIcons();			
			iconLayer.append("icon0", x, y, {
				name: $(".box_head_tab_room .name").text(),
				normalSrc: "/static/shared/images/icon/map/icon_home.png",
				clickMove: true,
				cursor: "pointer",
				scaleLevel: null,
				state: 'normal',
				normal: {
					leftOffset: -20,
					leftOffsetUnit: 'pixels',
					topOffset: 60,
					topOffsetUnit: 'pixels',
					scale: 1.0
				}
			});
		}
	};
	makeMap();


	/*******************************************************
	 * heightLine
	 *******************************************************/
	$(".article_index .around_article li").heightLine();
	$(".shop_info .heightLine").heightLine();
	
	//
	if(window.location.href.indexOf('#img_gallery') > 0){
		$('html, body').animate({
            scrollTop: $('#img_gallery').offset().top + 'px'
        }, 'fast');
	}
	
	//
	if($('#hidDetail') != undefined){
		RECENT.addToList($('#hidDetail').val());
	}
});
